package cn.renhui.hermesbus.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;

import java.util.concurrent.ConcurrentHashMap;

import cn.renhui.hermesbus.HarmesEventBusInterface;
import cn.renhui.hermesbus.Request;
import cn.renhui.hermesbus.Response;
import cn.renhui.hermesbus.core.HermesService;

public class ServiceConnectManager {

    // Class对应的链接对象
    private final ConcurrentHashMap<Class<? extends HermesService>, HermesServiceConnection> mHermesServiceConnections = new ConcurrentHashMap<>();

    // Class对应的Binder对象
    private final ConcurrentHashMap<Class<? extends HermesService>, HarmesEventBusInterface> mHermesServices = new ConcurrentHashMap<>();

    private ServiceConnectManager() {}

    private static final ServiceConnectManager instance = new ServiceConnectManager();

    public static ServiceConnectManager getInstance() {
        return instance;
    }

    public void bind(Context context, String packageName, Class<? extends HermesService> service) {
        LogUtils.e("service Name = " + service);
        HermesServiceConnection connection = new HermesServiceConnection(service);
        mHermesServiceConnections.put(service, connection);
        Intent intent;
        if (TextUtils.isEmpty(packageName)) {
            intent = new Intent(context, service);
        } else {
            intent = new Intent();
            intent.setClassName(packageName, service.getName());
        }
        context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    public Response request(Class<HermesService> hermesServiceClass, Request request) {
        LogUtils.e( "hermesServiceClass name" + hermesServiceClass);
        HarmesEventBusInterface harmesEventBusInterface = mHermesServices.get(hermesServiceClass);
        if (harmesEventBusInterface != null) {
            try {
                Response response = harmesEventBusInterface.send(request);
                return response;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 接受远端的 Binder对象, 进程B就可以通过Binder对象去操作服务端的方法
    private class HermesServiceConnection implements ServiceConnection {

        private Class<? extends HermesService> mClass;

        HermesServiceConnection(Class<? extends HermesService> service) {
            mClass = service;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            HarmesEventBusInterface hermesService = HarmesEventBusInterface.Stub.asInterface(service);
            mHermesServices.put(mClass, hermesService);
            LogUtils.e("Service Connected, key = " + mClass + ", value = " + hermesService);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mHermesServices.remove(mClass);
        }
    }

}
